package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 工程报装-工程收款-计算预缴金额
 * 预缴金额 = 预收款+进度款
 *
 * @author 朱超
 * @since 2022-01-19 10:44:39
 */
public class PjCollectionCountMoney implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            PjCollectionQuery pjCollectionQuery = JsonUtils.objectToPojo(req.getData(), PjCollectionQuery.class);

            // 1. 计算结果
            BigDecimal totalMoney = getTotalMoney(pjCollectionMapper, pjInfoMapper, pjCollectionQuery);
            // 2. 返回结果
            MessageBean<BigDecimal> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "预缴金额查询成功", BigDecimal.class);
            messageBean.setData(totalMoney);
            return messageBean;
        } catch (Exception e) {
            logger.error("预缴金额查询失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "预缴金额查询失败", null);
        }
    }

    public static BigDecimal getTotalMoney(PjCollectionMapper pjCollectionMapper, PjInfoMapper pjInfoMapper, PjCollectionQuery pjCollectionQuery) {
        // 1.1 预缴金额 = 预收款+进度款
        List<Integer> typeList = new ArrayList<>();
        typeList.add(1);
        typeList.add(2);
        pjCollectionQuery.setTypeList(typeList);
        // 修改处开始
        // 查询已收款状态的工程收款数据
        pjCollectionQuery.setStatus(2);
        // 修改处结束
        // 1.2 查询金额
        List<PjCollection> pjCollectionList = pjCollectionMapper.getList(pjCollectionQuery);
        BigDecimal collection = new BigDecimal(0);
        for (PjCollection pjCollection:pjCollectionList) {
            collection = collection.add(pjCollection.getMoney());
        }
//        // 2. 根据projectCode查询押金
//        BigDecimal deposit = pjInfoMapper.getDepositByProjectCode(pjCollectionQuery.getProjectCode());
//        // 3. 返回结果
//        BigDecimal totalMoney = collection.add(deposit);
        return collection;
    }
}
